package strategies;

import utils.EditorContext;
import utils.Title;

import java.util.ArrayList;
import java.util.List;

public class DirTreeStrategy extends ListTreeStrategy implements DisplayStrategy {
    private String dir;

    public DirTreeStrategy(String dir, EditorContext context) {
        super(context);
        this.dir = dir;
        getSubTitleList();
    }

    public void getSubTitleList() {
        int n = titleList.size();
        List<Title> subTitleList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (titleList.get(i).getText().equals(dir)) {
                subTitleList.add(titleList.get(i));
                for (int j = i + 1; j < n; j++) {
                    if (titleList.get(j).getLevel() > titleList.get(i).getLevel()) {
                        subTitleList.add(titleList.get(j));
                    } else {
                        break;
                    }
                }
            }
        }
        titleList = subTitleList;
        if (titleList.isEmpty()){
            System.out.println("No such directory.");
        }
    }
}
